# author: Han Zhang
library(ggplot2)
library(reshape2)


### read in Wickedonna raw (events level)
wicked = read.csv('wicked_events_2018-11-29.csv', sep = "\t", stringsAsFactors = F)
wicked$month = substr(wicked$date, 0, 7)

wicked_month_count = as.data.frame.table(table(wicked$month))
colnames(wicked_month_count) <- c("month",   "count")
wicked_month_count$Dataset = "Wickedonna"
wicked_month_count = wicked_month_count[3:(nrow(wicked_month_count)-4),]
wicked_month_count$quarter = as.numeric(substr(wicked_month_count$month,6,8))


##########################################################

# new event data

df = read.csv('../../output/protest_events.csv', sep = "\t", stringsAsFactors = F)

# df$date = sapply(strsplit(df$event_id, "_"), "[", 2)
# df$date <- as.Date(df$date)

df$date = substr(df$event_id, 8, 17)
df$month = substr(df$event_id, 8, 14)

df_month_count = as.data.frame.table(table(df$month))
colnames(df_month_count) <- c("month",   "count")
df_month_count$Dataset = "CASM"
df_month_count[1:(nrow(df_month_count)-1),]
df_month_count$quarter = as.numeric(substr(df_month_count$month,6,8))
df_month_count = df_month_count[df_month_count$month != "2017-07",]



dd = rbind(df_month_count, wicked_month_count)
dd$quarter = as.numeric(substr(dd$month,6,8))

dd$month_numeric <- as.numeric(as.factor(dd$month))


quar <- function(x,y) # x is quarter, y is dataset label
{
  if(x %% 3 == 1 & y == "CASM") 
  {
    if (x%/%3 == 0)
    {
      return ("Jan")
    }
    if (x%/%3 == 1)
    {return ("Apr")}
    if (x%/%3 == 2)
    {return ("Jul")}
    if (x%/%3 == 3)
    {return ("Oct")}
  }
  else {
    return ("");    
  }
}

dd$quarter <- mapply(quar, dd$quarter, dd$Dataset)
dd$year = substr(dd$month, 1,4)

quarter_label <- dd[dd$Dataset == "CASM", ]$quarter





cairo_pdf("CASM_Wickedonna.pdf", width = 12, height = 8)


g1 <- ggplot(dd, aes(x= month_numeric,
                     y = count, 
                     group = Dataset, shape = Dataset )) + 
  # geom_point() +
  geom_line(aes(linetype = Dataset)) +
  scale_shape_discrete() +
  geom_hline(aes(yintercept = 0), linetype = "dashed") +
  theme_bw() + 
  theme(panel.grid.major = element_blank(), panel.grid.minor = element_blank())  +
  theme(plot.margin = unit(c(1, 1, 4, 1), "lines"),
        axis.title.x = element_blank(),
        axis.text.x = element_blank(),
        axis.ticks.x=element_blank(),
        panel.grid.major.x = element_blank(),
        panel.grid.minor.x = element_blank()) +
  theme(axis.text.y= element_text(size = 18, colour = 'black'),
        axis.title.y=element_text(size = 20, colour = 'black'),
        legend.text = element_text(size = 18, colour = 'black'),
        legend.title  = element_text(size = 20, colour = 'black')) +
  theme(legend.position = c(0.78,0.91)) +
  ylab ("Monthly Count") + 
  xlab ("Month") + 
  coord_cartesian(ylim = c(-250, 3500), expand = T) + 
  annotate(geom = "text", x = seq_len(length(quarter_label)), y = - 150, label = quarter_label, size = 5) +
  annotate(geom = "text", x = 8 + 11 * (0: 7), y = - 300, label = unique(dd$year), size = 6) 

g2 <- ggplot_gtable(ggplot_build(g1))
g2$layout$clip[g2$layout$name == "panel"] <- "off"
grid::grid.draw(g2)

dev.off()





